package com.rk.kbk.common;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * 后台地图工具类
 * Created by Administrator on 2017/5/11 0011.
 */
public class MapUtils {
    private static String getResponse(String serverUrl){
        //用JAVA发起http请求，并返回json格式的结果
        StringBuffer result = new StringBuffer();
        try {
            URL url = new URL(serverUrl);
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line;
            while((line = in.readLine()) != null){
                result.append(line);
            }
            in.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    /**
     * 高德地图WebAPI 计算两地之间行驶的距离(米)<br/>
     * String origins:起始坐标<br/>
     * String destination:终点坐标
     */
   public static String distance(String origins, String destination) {
       String url = "http://restapi.amap.com/v3/direction/driving?" + "origin=" + origins + "&destination="
               + destination + "&strategy=" + 4 + "&extensions=base&key=fad9acf750531afd945e8ce3442b25bb";
       JSONObject jsonobject = JSONObject.fromObject(getResponse(url));
        JSONArray pathArray = jsonobject.getJSONObject("route").getJSONArray("paths");
        String distanceString = pathArray.getJSONObject(0).getString("distance");
        return distanceString;
    }

    /**
     * 高德地图WebAPI 计算多个地点之间行驶的距离(米)<br/>
     * String origins:起始坐标<br/>
     * String destination:终点坐标
     */
    public static List<String> manyDistance(String origins, String destination) {
      /* String url = "http://restapi.amap.com/v3/direction/driving?" + "origin=" + origins + "&destination="
               + destination + "&strategy=" + 4 + "&extensions=base&key=fad9acf750531afd945e8ce3442b25bb";*/
        String url = "http://restapi.amap.com/v3/distance?origins=" + origins + "&destination="+destination+"&output=json&type="+1+"&key=fad9acf750531afd945e8ce3442b25bb";
        JSONObject jsonobject = JSONObject.fromObject(getResponse(url));
        JSONArray pathArray = jsonobject.getJSONArray("results");
        List<String> list =new ArrayList<>();
        String distanceString;
        for(int i=0;i<pathArray.size();i++){
            distanceString = pathArray.getJSONObject(i).getString("distance");
            list.add(distanceString);
        }
        return list;
    }

    public static void main(String[] args) {
      String origin = "119.911279" + "," + "31.002662";
      String destination = "119.907677" + "," + "31.013172";
   //   String distance = distance(origin, destination);
        String latngFirst=119.907093+","+31.004727;
        String latngSecond=119.907677+","+31.013172;
        String latngThird=120.114361+","+30.333239;
        String str2="",str3="";
        String str=latngFirst+"|"+latngSecond;
        List<String> lengList= MapUtils.manyDistance(120.109961+","+30.334787,latngThird);
        for(int i=0,y=lengList.size();i<y;i++){
            BigDecimal bigDecimal=new BigDecimal(lengList.get(i)).divide(new BigDecimal(1000),2,BigDecimal.ROUND_HALF_UP);
            str2+= bigDecimal+"-";
        }
        if(!"".equals(str2)){
            str3=str2.substring(0,str2.length()-1);
        }
      System.out.println("行驶距离-----" + str3);

     }





}
